﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using G.WeiBo.WebAPI.Domain;
using G.WeiBo.WebAPI;
using G.WeiBo.WebAPI.Response;
using G.WeiBo.WebAPI.Request;

namespace G.WeiBo.View
{
    public partial class TopicPage : GBasePage
    {
        DefaultClient client = GlobalStaticData.Client;
        public TopicPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //listBoxWeeklyTopic.EnableTombstoneListBox<Topic>(this);
            //listBoxHourlyTopic.EnableTombstoneListBox<Topic>(this);
            //listBoxDailyTopic.EnableTombstoneListBox<Topic>(this);
            //if (this.NavigationMode == GWeiBo.Navigate.NavigationMode.New)
            //{
            //}
            GetDatas();
            //if ( listBoxDailyTopic.ItemsSource == null)
            //{
            //    this.listBoxDailyTopic.EnableTombstoneListBox<Topic>(this);
            //    GetDailyTopics();
            //}
            //if (listBoxWeeklyTopic.ItemsSource == null)
            //{
            //    this.listBoxWeeklyTopic.EnableTombstoneListBox<Topic>(this);
            //    GetWeeklyTopics();
            //}
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            this.NavigationService.Navigate(new Uri("/View/TopicBrowserPage.xaml?topicName=" + (listBox.SelectedItem as Topic).Name, UriKind.Relative));
        }
        private void GetHourlyTopics()
        {
            //this.listBoxHourlyTopic.BeginLoadingData();
            //client.Execute<GetTopicByTypeResponse>(new GetTopicBtTypeRequest { TopicType = TopicType.Hourly }, (c) =>
            //{
            //    this.Dispatcher.BeginInvoke(() =>
            //    {
            //        listBoxHourlyTopic.LoadData<Topic>(c.Topics);
            //    });
            //});
        }
        private void GetDailyTopics()
        {
            //this.listBoxDailyTopic.BeginLoadingData();
            //client.Execute<GetTopicByTypeResponse>(new GetTopicBtTypeRequest { TopicType = TopicType.Daily }, (c) =>
            //{
            //    this.Dispatcher.BeginInvoke(() =>
            //    {
            //        listBoxDailyTopic.LoadData<Topic>(c.Topics);
            //    });
            //});
        }

        private void GetWeeklyTopics()
        {
            //this.listBoxWeeklyTopic.BeginLoadingData();
            //client.Execute<GetTopicByTypeResponse>(new GetTopicBtTypeRequest { TopicType = TopicType.Weekly }, (c) =>
            //{
            //    this.Dispatcher.BeginInvoke(() =>
            //    {
            //        listBoxWeeklyTopic.LoadData<Topic>(c.Topics);
            //    });
            //});
        }

        private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetDatas();
        }
        private void GetDatas()
        {
            if ((panorama.SelectedIndex == 0 || panorama.SelectedIndex == -1) && listBoxMyTopic.ItemsSource == null)
            {
                listBoxMyTopic.EnableTombstoneListBox<UserTopic>(this);
            }
            //else if (panorama.SelectedIndex == 1 && listBoxHourlyTopic.ItemsSource == null)
            //{
            //    listBoxHourlyTopic.EnableTombstoneListBox<Topic>(this);
            //    GetHourlyTopics();
            //}
            //else if (panorama.SelectedIndex == 2 && listBoxDailyTopic.ItemsSource == null)
            //{
            //    this.listBoxDailyTopic.EnableTombstoneListBox<Topic>(this);
            //    this.GetDailyTopics();
            //}
            //else if (panorama.SelectedIndex == 3 && listBoxWeeklyTopic.ItemsSource == null)
            //{
            //    this.listBoxWeeklyTopic.EnableTombstoneListBox<Topic>(this);
            //    this.GetWeeklyTopics();
            //}
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }
        private void Refresh()
        {
            if (panorama.SelectedIndex == 0)
            {
                listBoxMyTopic.ClearItems();
                listBoxMyTopic.CancelPageListBox();
            }
            //else if (panorama.SelectedIndex == 1)
            //{
            //    listBoxHourlyTopic.ClearItems();
            //}
            //else if (panorama.SelectedIndex == 2)
            //{
            //    this.listBoxDailyTopic.ClearItems();
            //}
            //else if (panorama.SelectedIndex == 3)
            //{
            //    this.listBoxWeeklyTopic.ClearItems();
            //}
            GetDatas();
        }

        private void listBoxMyTopic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            this.NavigationService.Navigate(new Uri("/View/TopicBrowserPage.xaml?topicName=" + (listBox.SelectedItem as UserTopic).Name, UriKind.Relative));
        }

        private void listBoxMyTopic_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            client.Execute(new GetUserTopicsRequest { Count = e.PageSize, Page = e.PageNum, UserId = G.Net.Oauth.OAuth2AccessToken.CurrentToken.Uid }, (c) =>
            {
                listBoxMyTopic.LoadData<UserTopic>(c.Response);
            });
        }

        private void WatermarkTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox t = sender as TextBox;
            if (e.Key == Key.Enter)
            {
                this.Focus();
            }
            if (t.Text != string.Empty)
            {
                this.NavigationService.Navigate(new Uri("/View/TopicBrowserPage.xaml?topicName=" + t.Text, UriKind.Relative));
                t.Text = string.Empty;
            }
        }

        private void btnSearchTopic_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/View/TopicBrowserPage.xaml", UriKind.Relative));

        }

        private void menuDeleteTopic_Click(object sender, RoutedEventArgs e)
        {
            UserTopic topic = (sender as FrameworkElement).DataContext as UserTopic;
            client.Execute(new DeleteTopicRequest { TopicId = topic.TopicID }, (c) =>
            {
                if (!c.IsError)
                {
                    this.ShowState("删除成功！");
                    listBoxMyTopic.RemoveItem<UserTopic>(topic);
                }
            });
        }
    }
}